import OSS from 'ali-oss';


const  public_oss = new OSS({
    region: 'oss-cn-huhehaote',
    accessKeyId: 'LTAI5tKf1QAagtspveHGQZQV',
    accessKeySecret: 'tfHA8nUpqXG0QuxLBHn6XjPzdk46vw',
    bucket: 'transport-bt',
});



async function uploadFile(ossPath,localPath,type='public') {
    try {
      // 上传文件到OSS，'object'是OSS中的文件名，'localfile'是本地文件的路径。
      let uploadResult=null
      if(type=='public'){
        uploadResult = await public_oss.put(ossPath, localPath);
      }
      else{
        uploadResult= await private_oss.put(ossPath,localPath)
      }
      if(uploadResult.res.status === 200){
          console.log('上传成功');
          //检查url是否为http开头 替换成https
            if(uploadResult.url.indexOf('http://')>-1){
                uploadResult.url=uploadResult.url.replace('http://','https://');
            }
          return{code:1,path:uploadResult.name,url:uploadResult.url};
      }
        else{
            console.log('上传失败');
            return{code:0,msg:JSON.stringify(uploadResult)};
        }
    } catch (error) {
      console.error('阿里云oss上传出错:', error.message);
      return{code:0,msg:error.message};
      // 在此处添加错误处理逻辑。
    }
  }

export { uploadFile };
